package edu.hubu.talentmanagementsystem.auth;

import edu.hubu.talentmanagementsystem.common.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author moonlan
 * date 2021/3/19 下午4:52
 */
@SuppressWarnings(value = "unused")
public class SecurityAccessDeniedHandler implements AccessDeniedHandler {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        response.setStatus(403);
        logger.error(accessDeniedException.getMessage());
        CommonResult.write(request, response, new RuntimeException("Access Denied"));
    }
}
